package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.constant.CacheConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.result.Result;
import com.sky.service.ICategoryService;
import com.sky.service.ISetmealDishService;
import com.sky.service.ISetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private ISetmealService iSetmealService;

    @Autowired
    private ICategoryService iCategoryService;

    @Autowired
    private ISetmealDishService iSetmealDishService;

    @GetMapping("/page")
    public Result page(SetmealPageQueryDTO setmealPageQueryDTO){
        IPage<Setmeal> iPage =new Page<>(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        LambdaQueryWrapper<Setmeal> lqw =new LambdaQueryWrapper<>();
        lqw.like(setmealPageQueryDTO.getName()!=null&&!setmealPageQueryDTO.getName().trim().isEmpty(),
                Setmeal::getName,setmealPageQueryDTO.getName());
        lqw.eq(setmealPageQueryDTO.getCategoryId()!=null,Setmeal::getCategoryId,setmealPageQueryDTO.getCategoryId());
        lqw.eq(setmealPageQueryDTO.getStatus()!=null,Setmeal::getStatus,setmealPageQueryDTO.getStatus());
        iSetmealService.page(iPage,lqw);
        //套餐分类
        List<SetmealVO> voList =new ArrayList<>();
        List<Setmeal> records = iPage.getRecords();
        for (Setmeal record : records) {
            LambdaQueryWrapper<Category> lqw1 =new LambdaQueryWrapper<>();
            lqw1.eq(record.getCategoryId()!=null,Category::getId,record.getCategoryId());
            Category one = iCategoryService.getOne(lqw1);
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(record,setmealVO);
            setmealVO.setCategoryName(one.getName());
            voList.add(setmealVO);
        }
        IPage<SetmealVO> voiPage =new Page<>();
        voiPage.setRecords(voList);
        voiPage.setTotal(iPage.getTotal());
        return Result.success(voiPage);
    }

    @PostMapping
    @CacheEvict(cacheNames = CacheConstant.SETMEAL_CACHE,key = "#setmealDTO.categoryId")
    public Result add (@RequestBody SetmealDTO setmealDTO){
        iSetmealService.addSetmealAndDish(setmealDTO);
        return Result.success();
    }

    @PutMapping
    @CacheEvict(cacheNames = CacheConstant.SETMEAL_CACHE,allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        iSetmealService.updateSetmealAndDish(setmealDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = CacheConstant.SETMEAL_CACHE,allEntries = true)
    public Result status(@PathVariable Integer status,Long id){
        LambdaUpdateWrapper<Setmeal> luw =new LambdaUpdateWrapper<>();
        luw.set(Setmeal::getStatus,status).eq(Setmeal::getId,id);
        iSetmealService.update(luw);
        return Result.success();
    }

    @DeleteMapping
    @CacheEvict(cacheNames = CacheConstant.SETMEAL_CACHE,allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        iSetmealService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getSetmeal(@PathVariable Long id){
        Setmeal byId = iSetmealService.getById(id);
        SetmealDTO setmealDTO = new SetmealDTO();
        BeanUtils.copyProperties(byId,setmealDTO);
        //
        LambdaQueryWrapper<SetmealDish> lqw =new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = iSetmealDishService.list(lqw);
        setmealDTO.setSetmealDishes(list);
        return Result.success(setmealDTO);

        //最后需要返回一个SetmealDto
    }
}
